package com.kj.tms.system.controller.basicinfo;


import com.kj.tms.common.bean.dto.Pager;
import com.kj.tms.common.bean.dto.ReturnData;
import com.kj.tms.common.bean.dto.TreeData;
import com.kj.tms.common.utils.StringUtil;
import com.kj.tms.common.utils.constant.Consts;
import com.kj.tms.pri.system.basicinfo.dto.TaxIdentifyDto;
import com.kj.tms.pri.system.basicinfo.dto.TaxPayersInfoDto;
import com.kj.tms.pri.system.basicinfo.service.TaxPayersInfoService;
import com.kj.tms.pri.system.menu.dto.DictDto;
import com.kj.tms.pri.system.menu.service.DictService;
import com.kj.tms.pri.system.org.dto.AddressCityContryDto;
import com.kj.tms.pri.system.org.dto.OrgDto;
import com.kj.tms.pri.system.org.service.AddressCityContryService;
import com.kj.tms.pri.system.org.service.IndustryInfoService;
import com.kj.tms.pri.system.org.service.OrgService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dongshidong: 董世栋
 * @ProjectName: tms
 * @Package: com.hongtu.tms.basicinfo.controller;
 * @ClassName: TaxPayersInfoService
 * @Description: 纳税主体TaxPayersInfoController
 * @CreateDate: 2017/11/08 9:38
 * @UpdateUser:
 * @UpdateDate:
 * @UpdateRemark:
 * @Version: 1.0
 * Copyright: 北京宏图天安 Copyright (c) 2017
 */
@Controller
@RequestMapping("/basicinfo/taxPayersInfo")
public class TaxPayersInfoController {
    private static Logger logger = LoggerFactory.getLogger(TaxPayersInfoController.class);
    @Autowired
    private TaxPayersInfoService taxPayersInfoService;

    @Autowired
    private DictService dictService;

    @Autowired
    private AddressCityContryService addressCityContryService;

    @Autowired
    private IndustryInfoService industryInfoService;

    @Autowired
    private OrgService orgService;

    //纳税主体中使用字典类型
    //注册类型
    private  final String regType = "DJZCLX";
    //收入规模
    private  final String incomeScale = "SRGM";
    //纳税人资质
    private  final String taxpayerQualification = "NSRLX";
    //最高限额
    private  final String vatLimit = "ZZS_ZYFP_ZGXE";
    //税种编码
    private  final String taxCode = "SBSZ";

    /**
     * @param
     * @return String
     * @throws
     * @method index
     * @description 首页
     * @author 董世栋
     * @version 1.0
     * @date
     */
    @RequestMapping("/index")
    public String index() {
        return "basicinfo/taxPayersInfo/index";
    }

    /**
     * @param
     * @return ModelAndView
     * @throws
     * @method toAdd
     * @description 新增
     * @author 董世栋
     * @version 1.0
     * @date
     */
    @RequestMapping("/toAdd")
    public ModelAndView toAdd() {
        return new ModelAndView("basicinfo/taxPayersInfo/add", this.listByDictTypeS());
    }

    /**
     * @param model
     * @return ReturnData
     * @throws
     * @method listByPidCode
     * @description 查询数据
     * @author 董世栋
     * @version 1.0
     * @date
     */
    @RequestMapping("/listByPidCode")
    @ResponseBody
    public ReturnData listByPidCode(AddressCityContryDto model) {
        return new ReturnData(Consts.SUCCESS, "", addressCityContryService.listByPidCode(model.getId()));
    }

    /**
     * @param
     * @throws
     * @returnModelAndView
     * @method toUpdate
     * @description 更新
     * @author 董世栋
     * @version 1.0
     * @date
     */
    @RequestMapping("/toUpdate")
    public ModelAndView toUpdate(@RequestParam(value = "id")String id) {
        Map<String, Object> map = this.listByDictTypeS();
        map.put("id", id);
        return new ModelAndView("basicinfo/taxPayersInfo/update", map);
    }

    /**
     * @param
     * @return ModelAndView
     * @throws
     * @method toView
     * @description 查看
     * @author 董世栋
     * @version 1.0
     * @date
     */
    @RequestMapping("/toView")
    public ModelAndView toView(@RequestParam(value = "id")String id) {
        Map<String, Object> map = this.listByDictTypeS();
        map.put("id", id);
        return new ModelAndView("basicinfo/taxPayersInfo/view", map);
    }

    /**
     * @param
     * @return Map<String, Object>
     * @throws
     * @method queryTaxPayerByDictType
     * @description 根据字典类型查询
     * @author 董世栋
     * @version 1.0
     * @date
     */
    Map<String, Object> listByDictTypeS() {
        return dictService.listByDictTypeS(regType, incomeScale, taxpayerQualification, vatLimit);
    }

    /**
     * @param id
     * @return ReturnData
     * @throws
     * @method queryById
     * @description 根据ID获取数据对象
     * @author 董世栋
     * @version 1.0
     * @date
     */
    @RequestMapping("/queryById")
    @ResponseBody
    public ReturnData queryById(@RequestParam(value = "id",required = false) String id) {
        TaxPayersInfoDto dto = taxPayersInfoService.getByPrimaryKey(id);
        if (StringUtil.isNull(dto)) {
            return new ReturnData(Consts.ERROR, "没有找到对应的记录！", null);
        }
        return new ReturnData(Consts.SUCCESS, "", dto);
    }

    /**
     * @param
     * @return ReturnData
     * @throws
     * @method listTaxCode
     * @description 获取税种信息
     * @author 董世栋
     * @version 1.0
     * @date
     */
    @RequestMapping("/listTaxCode")
    @ResponseBody
    public ReturnData listTaxCode() {
        //税种信息
        List<DictDto> taxCodeList = dictService.listByDictType(taxCode);
        if (StringUtil.isNull(taxCodeList)) {
            return new ReturnData(Consts.ERROR, "没有找到对应的记录！", null);
        }
        return new ReturnData(Consts.SUCCESS, "", taxCodeList);
    }

    /**
     * @param dto
     * @return ReturnData
     * @throws
     * @method queryByPage
     * @description 查询分页数据
     * @author 董世栋
     * @version 1.0
     * @date
     */
    @RequestMapping("/queryByPage")
    @ResponseBody
    public ReturnData queryByPage(TaxPayersInfoDto dto) {
        Pager pager = taxPayersInfoService.queryByPage(dto);
        Map<String, Object> returnObject = new HashMap<String, Object>();
        returnObject.put("total", pager.getRowCount());
        returnObject.put("rows", pager.getResults());
        return new ReturnData(Consts.SUCCESS, "", returnObject);
    }

    /**
     * @param model
     * @return ReturnData
     * @throws
     * @method queryByList
     * @description 查询数据到列表
     * @author 董世栋
     * @version 1.0
     * @date
     */
    @RequestMapping("/queryByList")
    @ResponseBody
    public ReturnData queryByList(OrgDto model) {
        List<TreeData> dataList = null;
        try {
            dataList = orgService.queryByList(model);
        } catch (Exception e) {
            return new ReturnData(Consts.ERROR, "查询机构数据到列表失败", "");
        }
        //设置页面数据
        Map<String, Object> returnObject = new HashMap<String, Object>();
        returnObject.put("rows", dataList);
        return new ReturnData(Consts.SUCCESS, "", returnObject);
    }


    /**
     * @param dto
     * @return ReturnData
     * @throws
     * @method save
     * @description 添加或修改数据
     * @author 董世栋
     * @version 1.0
     * @date
     */
    @RequestMapping("/save")
    @ResponseBody
    public ReturnData save(TaxIdentifyDto dto) {
        ReturnData rtn = new ReturnData();
        if (StringUtil.isNotNull(dto.getId())) {
            return rtn = taxPayersInfoService.updateByPrimary(dto);
        } else {
            return rtn = taxPayersInfoService.insertDto(dto);
        }
    }

    /**
     * @param id
     * @return ReturnData
     * @throws
     * @method delete
     * @description 删除数据
     * @author 董世栋
     * @version 1.0
     * @date
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ReturnData delete(@RequestParam(value = "id",required = false) String id) {
        TaxPayersInfoDto dto = new TaxPayersInfoDto();
        dto.setId(id);
        dto.setValidMark(Consts.VALID_MARK_0);
        dto.setDelMark(Consts.DEL_MARK_1);
        taxPayersInfoService.deleteTaxPayersInfo(dto);
        return new ReturnData(Consts.SUCCESS, "删除成功~", null);
    }

    /**
     * @param
     * @return String
     * @throws
     * @method industryInfo
     * @description 行业信息页面
     * @author 董世栋
     * @version 1.0
     * @date
     */
    @RequestMapping("/industryInfo")
    public String industryInfo() {
        return "basicinfo/taxPayersInfo/industryInfo";
    }

    /**
     * @param request
     * @return ReturnData
     * @throws
     * @method listByPIndustryCode
     * @description 行业信息查询通过上级代码
     * @author 董世栋
     * @version 1.0
     * @date
     */
    @RequestMapping("/listByPindustryCode")
    @ResponseBody
    public ReturnData listByPindustryCode(HttpServletRequest request) {
        Map<String, Object> param = new HashMap<String, Object>();
        String id = (String)request.getParameter("id");
        List<TreeData> list = null;
        try {
            list = industryInfoService.listByPindustryCode("");
        } catch (Exception e) {
            return new ReturnData(Consts.ERROR, "获取行业信息失败", e.getMessage());
        }
        return new ReturnData(Consts.SUCCESS, "", list);
    }

    /**
     * @param model
     * @return ReturnData
     * @throws
     * @method getDirectChildrenTreeData
     * @description 获取组织机构树信息
     * @author 黄冬冬
     * @version 1.0
     * @date
     */
    @RequestMapping("/getDirectChildrenTreeData")
    @ResponseBody
    public ReturnData getDirectChildrenTreeData(OrgDto model) {
        List<TreeData> list = null;
        try {
            list = orgService.getDirectChildrenTreeData(model);
        } catch (Exception e) {
            return new ReturnData(Consts.ERROR, "获取组织机构树信息失败", e.getMessage());
        }
        return new ReturnData(Consts.SUCCESS, "", list);
    }

    /**
     * @param
     * @return String
     * @throws
     * @method relaOrg
     * @description 关联组织页面
     * @author 董世栋
     * @version 1.0
     * @date
     */
    @RequestMapping("/relaOrg")
    public String relaOrg() {
        return "basicinfo/taxPayersInfo/relaOrg";
    }
}





